package cn.piesat.service.impl;

import cn.piesat.dao.MailmsgConfigDAO;
import cn.piesat.dto.MailmsgConfigDTO;
import cn.piesat.service.intf.MailmsgConfigService;
import cn.piesat.util.JedisUtils;
import cn.piesat.util.PageUtil;
import cn.piesat.util.config.FieldStaticConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 手机短信 邮箱的一些方法    服务层实现
 * @author 奚好雷
 *
 */
@Service
public class MailmsgConfigServiceImpl implements MailmsgConfigService {
	
	@Autowired
	private MailmsgConfigDAO dao;

	@Override
	public MailmsgConfigDTO addSmsMessage(MailmsgConfigDTO dto) {
		MailmsgConfigDTO reDto = dao.save(dto);
		return reDto;
	}

	@Override
	public MailmsgConfigDTO addEmailMessage(MailmsgConfigDTO dto) {
		MailmsgConfigDTO reDto = dao.save(dto);
		return reDto;
	}

	@Override
	public Map<String, Object> getAllMessage(String configType,Integer page,Integer size) {
		Pageable pageable = PageRequest.of(page, size);
		Page<MailmsgConfigDTO> pageObject = dao.findByConfigType(configType,pageable);
		return PageUtil.getPage(pageObject);
	}

	@Override
	public List<MailmsgConfigDTO> getTypeAndStateMessage(String configType, Integer state) {
		return dao.findByConfigTypeAndEnableState(configType, state);
	}

	@Override
	@Transactional
	public MailmsgConfigDTO updateEmailMessage(MailmsgConfigDTO dto) {
		MailmsgConfigDTO reDTO = dao.save(dto);
		Jedis jedis = JedisUtils.getJedis();
		try {
			if (reDTO.getEnableState() == 0) {
				Map<String, String> map = new HashMap<>();
				map.put("spring.mail.host", reDTO.getDomain());
				map.put("spring.mail.username", reDTO.getUsername());
				map.put("spring.mail.password", reDTO.getPassword());
				map.put("spring.mail.default-encoding", "UTF-8");
				map.put("subject", reDTO.getSignName());
				Map<String, String> tempMap = JSON.parseObject(reDTO.getTemplate(), new TypeReference<Map<String,String>>(){});
				tempMap.forEach((k,v) -> map.put(k,v));
				jedis.hmset(FieldStaticConfig.MAILCONFIG_REDIS_KEY, map);
			} else if (reDTO.getEnableState() == 1) {
				List<MailmsgConfigDTO> list = dao.findByConfigTypeAndEnableState(reDTO.getConfigType(), 0);
				if(list.isEmpty() && jedis.exists(FieldStaticConfig.MAILCONFIG_REDIS_KEY)) {
					jedis.del(FieldStaticConfig.MAILCONFIG_REDIS_KEY);
				}
			}
		} finally {
			JedisUtils.close(jedis);
		}
		return reDTO;
	}

	@Override
	@Transactional
	public MailmsgConfigDTO updateSmsMessage(MailmsgConfigDTO dto) {
		MailmsgConfigDTO reDTO = dao.save(dto);
		
		Jedis jedis = JedisUtils.getJedis();
		try {
			if (reDTO.getEnableState() == 0) {
				Map<String, String> map = new HashMap<>();
				map.put("accessKeyId", reDTO.getAccessKeyId());
				map.put("accessSecret", reDTO.getAccessSecret());
				map.put("signName", reDTO.getSignName());
				map.put("domain", reDTO.getDomain());
				Map<String, String> tempMap = JSON.parseObject(reDTO.getTemplate(), new TypeReference<Map<String,String>>(){});
				tempMap.forEach((k,v) -> map.put("sms_" + k,v));
				jedis.hmset(FieldStaticConfig.SMSCONFIG_REDIS_KEY, map);
			} else if (reDTO.getEnableState() == 1) {
				List<MailmsgConfigDTO> list = dao.findByConfigTypeAndEnableState(reDTO.getConfigType(), 0);
				if(list.isEmpty() && jedis.exists(FieldStaticConfig.SMSCONFIG_REDIS_KEY)) {
					jedis.del(FieldStaticConfig.SMSCONFIG_REDIS_KEY);
				}
			}
		} finally {
			JedisUtils.close(jedis);
		}
		return reDTO;
	}

	@Override
	public void delById(String id) {
		MailmsgConfigDTO dto = dao.findById(id).get();
		dao.deleteById(id);
		if(dto.getEnableState() == 0) {
			Jedis jedis = JedisUtils.getJedis();
			try {
				switch (dto.getConfigType()) {
				case "sms":
					jedis.del(FieldStaticConfig.SMSCONFIG_REDIS_KEY);
					break;
				case "mail":
					jedis.del(FieldStaticConfig.MAILCONFIG_REDIS_KEY);
					break;	
				default:
					break;
				}
			} finally {
				JedisUtils.close(jedis);
			}
		}
	}

	@Override
	public boolean exists(String id) {
		return dao.existsById(id);
	}

	@Override
	public MailmsgConfigDTO findById(String id) {
		if (!dao.existsById(id)) {
			return null;
		}
		return dao.findById(id).get();
	}

	@Override
	public List<MailmsgConfigDTO> findByConfigTypeAndPlatFormType(String configType, String platFormType) {
		return dao.findByConfigTypeAndPlatFormType(configType, platFormType);
	}
	
}
